import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

const router = new Router({
    mode: 'history',
    linkActiveClass: 'active',
    routes: [
       {
           path: '/login',
           // 按需导入
           component: () => import('@/views/Login.vue')
       },
       {
           path: '/index',
           component: () => import('@/views/Index.vue'),
           redirect: '/index/dashboard',
           children: [
               {
                   path: '/index/dashboard',
                   component: () => import('@/views/Index/Dashboard.vue')
               },
               {
                    path: '/index/user',
                    component: () => import('@/views/Index/User.vue')
               },
               {
                    path: '/index/role',
                    component: () => import('@/views/Index/Role.vue')
               },
               {
                    path: '/index/perm',
                    component: () => import('@/views/Index/Permission.vue')
               }
           ]
       },
       {
           path: '/',
           component: () => import('@/views/Index.vue')
       }
    ]
})

/**
 * 类似于Java中的过滤器
 * to: 到哪里去
 * from: 从哪里来，不用
 * next: 方法，是否要接着往下走，如果没有参数表示接着往下走；如果参数(是路由path), 就到对应的页面
 */
/**
router.beforeEach((to, from, next) => {
    // to.path 获取到路由的路径 /login /index /home /about
    // to.path = '/login' 表示到登录页面，直接放它过去
    // cookie的用法有两种： this.$cookies  Vue.$cookies, this只能在组件中使用
    if(to.path === '/login' || (Vue.$cookies.get('access_token') && Vue.$cookies.get('refresh_token'))) {
        next()
    }else {
        next('/login')  // 表示到登录页面
    }
})
*/

export default router
